<template>
	<view class="list-view">
		<view class="top-bar"><uni-navbar :show="show" @click-show="show = !show" :fixed="true" :back="true" color="#fff" title="问题草稿"></uni-navbar></view>

		<view class="filter-box" v-if="show">
			<view class="form-box">
				<u-form ref="uForm">
					<u-form-item class="form-item" label="问题内容">
						<u-input v-model="will.opinionDescribe" type="text" placeholder="请输入问题内容" />
					</u-form-item>
					<u-form-item class="form-item" label="问题编号">
						<u-input v-model="will.opinionNo" type="text" placeholder="请输入问题编号" />
					</u-form-item>
					<u-form-item class="form-item" label="问题类型">
						<u-input v-model="type" type="select" :select-open="typeShow" placeholder="请选择类型" @click="typeShow = true" />
						<u-select :list="typeList" v-model="typeShow" @confirm="typeConfirm"></u-select>
					</u-form-item>
					<u-form-item class="form-item" label="提交人">
						<u-input v-model="will.createName" type="text" placeholder="请输入提交人" />
					</u-form-item>
				</u-form>
			</view>
			<view class="fiexd-btm">
				<view class="flex-box">
					<u-button class="form-btn" type="default" @click="resert">清除</u-button>
					<u-button class="form-btn" type="error" @click="filter">确定</u-button>
				</view>
			</view>
		</view>

		<view class="search-view">
			<u-search class="search-input" shape="square" bg-color="#fff" v-model="will.opinionDescribe" :show-filter="true" @search="search" @custom="show = !show"></u-search>
		</view>
		<u-tabs class="tab-box" :list="tabsList" :current="tabsCurrent" @change="tabsChange" :is-scroll="true"></u-tabs>
		<block v-if="!$roleCodes('qunzhong')">
			<view class="tab-box" v-if="[1, 2, 3].indexOf(tabsCurrent) > -1">
				<view class="tab-nav" v-if="tabsCurrent == 1">
					<view class="tab-item" :class="tabIndex == 20 ? 'active' : ''" @click="tab(20)">待审查</view>
					<view class="tab-item" :class="tabIndex == 21 ? 'active' : ''" @click="tab(21)">待确定</view>
				</view>
				<view class="tab-nav" v-if="tabsCurrent == 2">
					<view class="tab-item" :class="tabIndex == 30 ? 'active' : ''" @click="tab(30)">待答复</view>
					<view class="tab-item" :class="tabIndex == 31 ? 'active' : ''" @click="tab(31)">待确定</view>
				</view>
				<view class="tab-nav" v-if="tabsCurrent == 3">
					<view class="tab-item" :class="tabIndex == 40 ? 'active' : ''" @click="tab(40)">待操作</view>
					<view class="tab-item" :class="tabIndex == 41 ? 'active' : ''" @click="tab(41)">待确定</view>
				</view>
			</view>
		</block>

		<view class="list-box" :style="{ background: loading == 'nodata' ? 'none' : '#fff' }">
			<view class="list-info_01" v-for="(item, index) of willList" :key="item.id" @click="navTo(item)">
				<view class="info-title">{{ item.opinionDescribe }}</view>
				<view class="info-btm">
					<view class="btm-left clamp">
						<text class="left-name">{{ item.createName }}</text>
						<text class="left-time">{{ item.createTime }}</text>
					</view>
					<view class="btm-right">
						<view class="right-type" :style="'color:' + colorMap[item.nodeStatus]">{{ item.nodeStatusName }}</view>
						<view class="right-btn" v-if="item.nodeStatus == 1 && item.createName == userInfo.username" @click.stop="del(item)">删除</view>
					</view>
				</view>
			</view>
			<u-loadmore :status="loading" />
		</view>

		<u-back-top :scroll-top="scrollTop" top="200"></u-back-top>
	</view>
</template>

<script>
import dateTimePicker from '@/components/uni-datetime-picker';
import { mapState } from 'vuex';
export default {
	components: {
		dateTimePicker
	},
	data() {
		return {
			scrollTop: 0,
			show: false,
			tabIndex: 0,
			tabsCurrent: 0,
			tabsList: [
				{
					name: '所有',
					value: 0
				},
				{
					name: '待审查',
					value: 1
				},
				{
					name: '办理中',
					value: 2
				},
				{
					name: '已答复',
					value: 3
				},
				{
					name: '已办结',
					value: 4
				},
				{
					name: '不予受理',
					value: 5
				}
			],
			statusMap: {
				0: '所有',
				1: '待审查',
				2: '办理中',
				3: '已答复',
				4: '已办结',
				5: '不予受理'
			},
			colorMap: {
				0: '#0099ff',
				1: '#ff9900',
				2: '#19be6b',
				3: '#00ff00',
				4: '#00ffff',
				5: '#F5A623'
			},
			status: 0,
			title: '',

			type: '',
			typeShow: false,
			typeList: [],

			createTime: '',
			endTime: '',

			willList: [],

			will: {
				opinionNo: '',
				opinionType: '',
				phone: '',
				stationId: '',
				createName: '',
				nodeStatus: '0',
				opinionDescribe: ''
			},
			loading: 'loadmore'
		};
	},
	onLoad(option) {
		this.loadLogin();
		if (option && option.status) {
			this.status = option.status;
			this.will.nodeStatus = option.status;
			this.tabsCurrent = option.status;
		}
		if (option && option.stationId) {
			// this.will.stationId = option.stationId;
		}
		if (option && option.type) {
			this.type = option.type;
			this.will.opinionType = option.type;
		}
		this.loadData();
		this.loadDict('mqzt_type', 'typeList');
	},
	onPullDownRefresh() {
		this.loadData('refresh');
	},
	onReachBottom() {
		if (this.loading == 'loadmore') this.loadData('add');
	},
	onPageScroll(res) {
		this.scrollTop = res.scrollTop;
	},
	computed: {
		...mapState(['userInfo'])
	},
	methods: {
		loadLogin() {
			if (this.userInfo.sysUser) {
				this.clientCode = this.userInfo.sysUser.clientCode;
				this.userId = this.userInfo.sysUser.id;
				this.roleCodes = this.userInfo.roleCodes.join(',');
				this.will.reflectUser = this.userInfo.sysUser.id;

				this.pageNum = 1;
				this.pageSize = 10;
			} else {
				this.$logout();
			}
		},
		typeConfirm(e) {
			this.type = e[0].label;
			this.will.opinionType = e[0].value;
		},
		loadDict(type, list) {
			this.$request('/system/sys-dict/dictList', {
				clientCode: this.clientCode,
				sysDict: {
					dictType: type
				}
			}).then((res) => {
				if (!res.data) {
					return false;
				}
				this[list] = [];
				res.data.forEach((item) => {
					this[list].push({
						value: item.dictValue,
						label: item.dictName
					});
				});
			});
		},
		loadData(type = 'add') {
			if (this.loading == 'loading') {
				return false;
			}
			if (type == 'add') {
				if (this.loading == 'nomore') {
					return false;
				}
				this.loading = 'loading';
			} else {
				this.loading = 'loading';
			}
			if (type != 'add') {
				this.pageNum = 1;
			}
			if (this.status == 0) {
				this.will.nodeStatus = '';
			}
			let data = {
				pageNum: this.pageNum,
				pageSize: this.pageSize,
				userId: this.userId,
				clientCode: this.clientCode,
				role: this.roleCodes,
				...this.will
			};
			let url = '/minqingzhitong/msPublicOpinion/getCleckList';
			if (this.status == 5) {
				url = '/minqingzhitong/msPublicOpinion/getInadmissibleList';
			}

			if (!this.$roleCodes('qunzhong')) {
				console.log(this.tabIndex);
				if (this.tabsCurrent == 1) {
					data.handlingResulstFlag = this.tabIndex - 19 + '';
				}
				if (this.tabsCurrent == 2) {
					data.handelFlag = this.tabIndex - 30 + '';
				}
				if (this.tabsCurrent == 3) {
					data.handelFlag = this.tabIndex == 40 ? '2' : '3,4';
				}
			}

			this.$request(url, data, {
				load: true
			}).then((res) => {
				let count = 0;
				if (type != 'add') {
					uni.stopPullDownRefresh();
					this.willList = [];
				}
				this.loading = this.pageNum < Math.ceil(res.data.total / this.pageSize) ? 'loadmore' : 'nomore';
				this.pageNum = this.pageNum + 1;
				this.willList = this.willList.concat(res.data.records);
				this.loaded = true;
				if (this.willList.length <= 0) {
					this.loading = 'nodata';
				}
			});
		},
		timeChange({ date, dt }, idx) {
			this[idx] = date.year + '-' + date.month2 + '-' + date.day2;
		},
		tab(e) {
			this.tabIndex = e;
			this.loadData('refresh');
		},
		tabsChange(index) {
			this.tabsCurrent = index;
			this.tabIndex = index * 10 + 10;
			this.typeFunc(this.tabsList[index].value);
		},
		del(e) {
			let url = '/minqingzhitong/msPublicOpinion/del';
			let data = {
				id: e.id
			};

			uni.showModal({
				title: '提示',
				content: '确认删除？',
				success: (res) => {
					if (res.confirm) {
						this.$request(url, data, {
							load: true
						}).then((res) => {
							this.$msg(res.msg);
							this.loadData('refresh');
						});
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});
		},
		filter() {
			this.show = false;
			this.loadData('refresh');
		},
		resert() {
			this.show = false;
			this.will.opinionDescribe = '';
			this.will.opinionNo = '';
			this.type = '';
			this.will.opinionType = '';
			this.will.createTime = '';
			this.loadData('refresh');
		},
		search(e) {
			this.will.title = e;
			this.loadData('refresh');
		},
		typeFunc(e) {
			this.status = e;
			this.will.nodeStatus = e.toString();
			this.loadData('refresh');
		},
		navTo(item) {
			uni.navigateTo({
				url: '/pages/condi/detail?id=' + item.id
			});
		}
	}
};
</script>

<style lang="scss">
.tab-box {
	padding: 18rpx 0;
	font-size: 14px;
	color: #666;
	line-height: 60rpx;
	overflow-x: auto;
	background-color: #fff;

	& + .tab-box {
		padding-top: 0;
	}

	.search-filter {
		margin: 0 30rpx;
		width: 120rpx;
		height: 60rpx;
		float: right;
		text-align: center;
		border-radius: 6rpx;
		background-color: #e6e7e8;
	}

	.title {
		padding: 0 30rpx;
		float: left;
		font-weight: bold;
	}
}

.tab-nav {
	justify-content: space-around;
	padding: 0 20rpx;
	border: 0;
	background-color: #fff;

	&:after {
		// content: '';
		flex-shrink: 0;
		width: 20rpx;
		height: 1rpx;
	}

	.tab-item {
		flex-shrink: 0;
		margin: 0 10rpx;
		padding: 0 20rpx;
		max-width: inherit;
		width: auto;
		height: 60rpx;
		color: #666;
		text-align: center;
		border-radius: 6rpx;
		background-color: #e6e7e8;
	}

	.tab-item.active {
		font-weight: bold;
		color: #fff;
		background-color: #d0021b;
	}
}

.list-box {
	padding: 0 30rpx;
	background-color: #fff;
}

.filter-box {
	position: fixed;
	left: 0;
	top: 0;
	bottom: 0;
	right: 0;
	z-index: 998;
	background-color: #fff;
}

.form-box {
	.form-item {
		@include mp-flex;
		border-bottom: 20rpx solid #f5f7fa;
	}
}

.fiexd-btm {
	position: static;
	padding: 30rpx;
}
</style>
